package com.yxq.mdt.app.modules.spike.model;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.App;
import com.yxq.mdt.app.base.RespBase;
import com.yxq.mdt.app.base.ibase.IBaseModel;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.modules.spike.bean.SpikeBean;
import com.yxq.mdt.app.modules.spike.bean.SpikeGoodsBean;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SpikeModel implements IBaseModel {

    public void list(ApiModel.CommonCallback<SpikeBean> callback) {
        RetrofitFactory.getRetrofit().create(SpikeApi.class)
                .list(Constant.getToken())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
//                            String data = App.getInstance().getResources().getString(R.string.text_spike);

//                            Gson gson = new Gson();
//                            RespBase<String> res = gson.fromJson(data, RespBase.class);
//                            RespBase<String> res = JSON.parseObject(data, RespBase.class);
//                            Log.e("data", data);

                            SpikeBean spikeBean = JSON.parseObject(respBase.getData(), SpikeBean.class);
                            callback.onSuccess(spikeBean);
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void goodsList(String time, int page, ApiModel.CommonCallback<SpikeGoodsBean> callback) {
        RetrofitFactory.getRetrofit().create(SpikeApi.class)
                .goodsList(Constant.getToken(), time, page)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<SpikeGoodsBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<SpikeGoodsBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {

    }
}
